<?php
/*
 * @Author: LiuHaiL 18655112670@139.com
 * @Date: 2022-07-25 10:58:29
 * @LastEditors: LiuHaiL 18655112670@139.com
 * @LastEditTime: 2022-12-05 17:32:08
 * @FilePath: \ant_form_admind:\laragon\www\ghpt_api\app\api\command\v1\syncAuditExam.php
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
declare (strict_types=1);

namespace app\api\command\v1;

use app\api\model\v1\AuditExam;
use app\api\model\v1\AuditExamFile;
use think\cache\driver\Redis;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Config;

class syncAuditExam extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('syncauditexam')
            ->setDescription('the syncauditexam command');
    }

    protected function execute(Input $input, Output $output)
    {
        $config_redis = Config::get('redis');
        $redis = new Redis($config_redis);
        //$redis->connect("127.0.0.1", 6379);
        $redis_key = 'audit_exam';
        $redis_key_arr = $redis->keys($redis_key . '*');
        if (!empty($redis_key_arr)){
            foreach ($redis_key_arr as $key => $value) {
                $data = $redis->ZRANGE($value, 0, -1,['WITHSCORES'=>true]);
                foreach ($data as $k => $v) {
                    $json_arr = json_decode($k, true);
                    $output->writeln(sprintf('处理数据 - 对应的id'.$v.'-redis-key:'.$value));
                    $res = AuditExam::where(['exam_id' => $json_arr['exam_id'], 'date_of' => $json_arr['date_of'], 'unit_id' => $json_arr['unit_id'], 'user_id' => $json_arr['user_id']])->findOrEmpty();
                    if ($res->isEmpty()) {
                        //创建
                        $audit_exam_id = AuditExam::create([
                            'industry_id' => $json_arr['industry_id'] ?? 0,
                            'unit_id' => $json_arr['unit_id'],
                            'user_id' => $json_arr['user_id'],
                            'exam_id' => $json_arr['exam_id'],
                            'start_time' => $json_arr['time'] ?? time(),
                            'exam_creation_type' => $json_arr['exam_creation_type'],
                            'date_of' => $json_arr['date_of'],
                            'attribute' => $json_arr['attribute'] ?? 0
                        ])->id;
                    } else {
                        $audit_exam_id = $res['id'];
                    }
                    $file = AuditExamFile::where(['audit_exam_id' => $audit_exam_id, 'exam_detail_id' => $json_arr['detail_id'], 'date_of' => $json_arr['date_of'], 'unit_id' => $json_arr['unit_id'], 'user_id' => $json_arr['user_id']])->findOrEmpty();
                    $data_file = [
                        'audit_exam_id' => $audit_exam_id,
                        'unit_id' => $json_arr['unit_id'],
                        'user_id' => $json_arr['user_id'],
                        'exam_id' => $json_arr['exam_id'],
                        'exam_stair_type_id' => $json_arr['exam_stair_type_id'],
                        'exam_second_level_id' => $json_arr['exam_second_level_id'],
                        'exam_detail_id' => $json_arr['detail_id'],
                        'file_path' => json_encode($json_arr['img_path'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE),
                        'content' => $json_arr['content'] ?? '',
                        'date_of' => $json_arr['date_of'],
                        'attribute' => $json_arr['attribute'] ?? 0
                    ];
                    if (!$file->isEmpty($file)) {
                        $file->save($data_file);
                    } else {
                        AuditExamFile::create($data_file);
                    }
                    //删除
                    $redis->ZREM($value,$k);

                }
                $output->writeln(sprintf('处理完成 -redis-key:'.$value));
            }
            sleep(1); //睡眠1秒
            // 指令输出
            $output->writeln('同步答题');
        }else{
            // 指令输出
            $output->writeln('redis 不存在数据');
        }


    }
}
